package com.zc.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.zc.common.Result;
import com.zc.dto.PasswordUpdateDTO;
import com.zc.exception.CustomerException;
import com.zc.pojo.Admin;
import com.zc.service.AdminService;
import jakarta.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zc
 * @Date 2025/8/19
 * @apiNote
 */
@RestController
public class WebController {
    @Resource
    private AdminService adminService;

    @PostMapping("/insetAdmin")
    public Result insetAdmin() {
        Admin admin = new Admin();
        LambdaQueryWrapper<Admin> lmq = new LambdaQueryWrapper<>();
        lmq.eq(Admin::getUsername, "admin");
        Admin one = adminService.getOne(lmq);
        if (one != null) {
            throw new CustomerException("管理员已存在");
        }
        admin.setUsername("admin");
        admin.setPassword("admin");
        admin.setName("管理员");
        admin.setRole("ADMIN");
        return Result.success(adminService.save(admin));
    }

    @PostMapping("/login")
    public Result login(@RequestBody Admin admin) {
        LambdaQueryWrapper<Admin> lmq = new LambdaQueryWrapper<>();
        lmq.eq(Admin::getUsername, admin.getUsername());
        Admin one = adminService.getOne(lmq);
        if (one == null) {
            throw new CustomerException("账号不存在");
        }
        if (!one.getPassword().equals(admin.getPassword())) {
            throw new CustomerException("账号或密码错误");
        }
        return Result.success(one);
    }


    @GetMapping("/youkeLogin")
    @Transactional
    public Result youkeLogin() {
        LambdaQueryWrapper<Admin> lmq = new LambdaQueryWrapper<>();
        lmq.eq(Admin::getUsername, "youke");
        Admin one = adminService.getOne(lmq);
        if (one == null) {
            one = new Admin(); // 先初始化对象，再设置属性
            one.setName("游客");
            one.setUsername("youke");
            one.setPassword("youke"); // 实际项目中建议加密存储
            one.setRole("USER");
            // 可以补充其他必要的默认值，如status=1（正常状态）
            one.setStatus(1);
            adminService.save(one);
        }

        return Result.success(one);
    }

    @PostMapping("/register")
    public Result register(@RequestBody Admin admin) {
        LambdaQueryWrapper<Admin> lmq = new LambdaQueryWrapper<>();
        lmq.eq(Admin::getUsername, admin.getUsername());
        Admin one = adminService.getOne(lmq);
        if (one != null) {
            throw new CustomerException("账号已存在");
        }
        if (admin.getPassword() == null) {
            throw new CustomerException("密码不能为空");
        }
        if (StrUtil.isBlank(admin.getName())) {
            admin.setName(admin.getUsername());
        }
        admin.setRole("USER");
        return Result.success(adminService.save(admin));
    }

    @PostMapping("/updatePassword")
    public Result updatePassword(@RequestBody PasswordUpdateDTO dto) {
        if (dto.getId() == null) {
            throw new CustomerException("ID不能为空");
        } else if (dto.getPassword() == null) {
            throw new CustomerException("密码不能为空");
        } else if (dto.getNewPassword() == null) {
            throw new CustomerException("新密码不能为空");
        }
        Admin one = adminService.getById(dto.getId());
        if (one == null) {
            throw new CustomerException("用户不存在");
        }
        if (!dto.getPassword().equals(one.getPassword())) {
            throw new CustomerException("密码或账号错误！");
        }
        if (dto.getNewPassword().equals(one.getPassword())) {
            throw new CustomerException("新密码不能和原密码重复！");
        }
        one.setPassword(dto.getNewPassword());
        return Result.success(adminService.updateById(one));
    }

    @PostMapping("/updatePerson")
    public Result updatePerson(@RequestBody PasswordUpdateDTO dto) {
        if (dto.getId() == null) {
            throw new CustomerException("ID不能为空");
        }
        Admin one = adminService.getById(dto.getId());
        if (one == null) {
            throw new CustomerException("用户不存在");
        }

        one.setName(dto.getName());
        return Result.success(one);
    }
}
